package com.cskaoyan.service;

import com.cskaoyan.bean.po.*;
import com.cskaoyan.mapper.MarketCouponMapper;
import com.cskaoyan.mapper.MarketCouponUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @ClassName:CouponServiceImpl
 * @Auther: HeXianZu
 * @Description:
 * @Date: 2022/10/11 20:20
 */
@Service
public class CouponServiceImpl implements CouponService {

    @Autowired
    MarketCouponMapper marketCouponMapper;
    @Autowired
    MarketCouponUserMapper marketCouponUserMapper;

    @Override
    public List<MarketCoupon> list(BaseParam baseParam, MarketCoupon marketCoupon) {

        MarketCouponExample example = new MarketCouponExample();
        MarketCouponExample.Criteria criteria = example.createCriteria();
        String name = marketCoupon.getName();
        Short type = marketCoupon.getType();
        Short status = marketCoupon.getStatus();
        if (!StringUtils.isEmpty(name)) {
            criteria.andNameLike("%" + name.trim() + "%");
        }
        if (!StringUtils.isEmpty(type)) {
            criteria.andTypeEqualTo(type);
        }
        if (!StringUtils.isEmpty(status)) {
            criteria.andStatusEqualTo(status);
        }
        List<MarketCoupon> marketCoupons = marketCouponMapper.selectByExample(example);
        return marketCoupons;
    }

    @Override
    public MarketCoupon create(MarketCoupon marketCoupon) {

        int i = marketCouponMapper.insertSelective(marketCoupon);
        if (i == 1) {
            MarketCoupon marketCoupon1 = marketCouponMapper.selectByPrimaryKey(marketCoupon.getId());
            return marketCoupon1;
        }
        return null;
    }

    @Override
    public List<MarketCouponUser> listUser(BaseParam baseParam,Integer couponId,Integer userId,Short status) {
        MarketCouponUserExample example = new MarketCouponUserExample();
        MarketCouponUserExample.Criteria criteria = example.createCriteria();
        if (couponId!=null){
            criteria.andIdEqualTo(couponId);
        }
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        if (status!=null){
            criteria.andStatusEqualTo(status);
        }

        List<MarketCouponUser> marketCouponUsers = marketCouponUserMapper.selectByExample(example);
        return marketCouponUsers;
    }

    @Override
    public MarketCoupon update(MarketCoupon marketCoupon) {
        int i = marketCouponMapper.updateByPrimaryKey(marketCoupon);
        if (i == 1) {
            MarketCoupon marketCoupon1 = marketCouponMapper.selectByPrimaryKey(marketCoupon.getId());
            return marketCoupon1;
        }
        return null;
    }

    @Override
    public int delete(MarketCoupon marketCoupon) {
        int i = marketCouponMapper.deleteByPrimaryKey(marketCoupon.getId());
        if (i == 1) {
            return 200;
        }
        return 400;
    }

    @Override
    public MarketCoupon read(Integer id) {
        MarketCoupon marketCoupon = marketCouponMapper.selectByPrimaryKey(id);
        return marketCoupon;
    }
}
